package com.fare.taluo.web.action;

import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import javax.annotation.Resource;
import javax.servlet.ServletException;

import net.sf.json.JsonConfig;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.fare.taluo.BaseFdcAction;
import com.fare.taluo.application.FreightPriceAnalyseAlertConfigApplication;
import com.fare.taluo.application.PageUtilApplication;
import com.fare.taluo.application.plane.channel.SourceApplication;
import com.fare.taluo.application.plane.city.CityQueryApplication;
import com.fare.taluo.application.plane.city.PortApplication;
import com.fare.taluo.auth.security.LogonUserCertificate;
import com.fare.taluo.util.DateJsonValueProcessor;
import com.fare.taluo.util.I18nUtil;
import com.fare.taluo.util.StringAndDateAndFileUtil;
import com.fare.taluo.vo.core.AirCompanyEntityVO;
import com.fare.taluo.vo.core.CityEntityVO;
import com.fare.taluo.vo.core.FreightPriceAnalyseAlertConfigVo;
import com.fare.taluo.vo.core.SourceEntityVO;
import com.fare.util.DateUtils;

@Controller("freightPriceAnalyseAlertConfigAction")
@Scope("prototype")
public class FreightPriceAnalyseAlertConfigAction extends BaseFdcAction{
	/**
	 * 
	 */
	private static final long serialVersionUID = -7422522870524996309L;
	private FreightPriceAnalyseAlertConfigVo fvo;
	private Long id;//这个名字你懂的
	private String flightFrom;//出发地
	private String flightTo;//目的地
	private int alertWay;
	private String effectiveFrom;//预警起始时间
	private String effectiveTo;//预警结束
	private String airType;//航程类型
	private String bookingType;//订票类型改为暂存最低价预警中的以下或以下条件
	private String efd;
	private String etd;
	private String endCreateTimeStr;
	private Long creator;
	private String creatorName;
	private Date createTime ;
	private Date endCreateTime;
	private String oldcreateTime;
	private String chooseUpOrdown;
	private int futureDays;
	private int triggerCondition;
	private String airCompany;
	private int queryJump=1;//是否是查询跳转的_yunjiayujing.jsp ，1==不是  0==是
	
	private List<AirCompanyEntityVO> sourceList;
	@Resource(name="pageUtilApplication")
	private PageUtilApplication pageUtilApplication;
	@Resource(name="sourceApplication")
	private SourceApplication sourceApplication;
	
	@Resource(name="cityQueryApplication")
	private CityQueryApplication cityQueryApplication;
	
	private List<SourceEntityVO>  sourceVOList;
	private String isUpdate;
	//默认当前页是首页
	private int count = 1;
	private int flag;
	//默认显示的数据是从第1个开始，第15个结束
	private int begin;
	private int end;
	//分页显示一页的数据量
	private final int pageSize = 15;
	private int total;
	private List<FreightPriceAnalyseAlertConfigVo> listvo;//查询预警配置集合
	private FreightPriceAnalyseAlertConfigVo vo;
	private String data;//json字符串 
	private String message;
	
	@Resource(name = "freightPriceAnalyseAlertConfigApplication")
    private FreightPriceAnalyseAlertConfigApplication freightPriceAnalyseAlertConfigApplication;
	@Resource(name = "portApplication")
	private PortApplication portApplication;
	@Resource(name="sourceApplication")
	private SourceApplication sourceApp;
	
	private String language;
	private String carrierKey;
	private String flightNo;
	private String cabinLevel;
	private String flightDate;
	private String companyCode;
	private int triggerConditionSpread;
	private String contrastSourceKey;
	private String compareCompanyCode;
	private String contrastFlightNo;
	private String contrastCabinLevel;
	private String alertType;
	private String createTimeStr;
	private String fromCity;
	private String toCity;
	private String fromCityCode;
	private String toCityCode;
	//运价预警 航空公司下拉框
	private List<SourceEntityVO> channelSourceList = new ArrayList<SourceEntityVO>();
	private List<SourceEntityVO> tempList=new ArrayList<SourceEntityVO>();
	
	public FreightPriceAnalyseAlertConfigApplication getFreightPriceAnalyseAlertConfigApplication() {
		return freightPriceAnalyseAlertConfigApplication;
	}

	public void setFreightPriceAnalyseAlertConfigApplication(
			FreightPriceAnalyseAlertConfigApplication freightPriceAnalyseAlertConfigApplication) {
		this.freightPriceAnalyseAlertConfigApplication = freightPriceAnalyseAlertConfigApplication;
	}
	
	public void init(){
		sourceList = pageUtilApplication.carriers();
		/*sourceVOList = sourceApplication.findAllSourceEntity();*/
		
		
		LogonUserCertificate user = com.fare.taluo.auth.security.ContextLoginUser.getLoginUser();
		
		this.channelSourceList=this.sourceApp.findAllSourceEntity();
		Locale locale = super.getLocale();
		I18nUtil.sourceConvert2I18N(channelSourceList, locale);
		
		//获得用户所在公司，用于前台显示
		for (SourceEntityVO element : channelSourceList) {
			if(element.getHbaseKey().equals(user.getSourceKey())){
				tempList.add(element);
			}
		}
		language=locale.toString();
		if(!language.equalsIgnoreCase("zh_CN") && !language.equalsIgnoreCase("en_US")){
			language="other";
		}
	}
	
	public String toSave(){
		init();
		this.returnPageURL = "/fare/rateAnalysis/_saveYunjiaPanel.jsp";
		return "dispatcher";
	}
	
    /**
     * 保存最低价预警配置
     */
	public void saveFreightPriceAnalyseAlertConfig() throws IOException{
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			LogonUserCertificate user = com.fare.taluo.auth.security.ContextLoginUser.getLoginUser();

			FreightPriceAnalyseAlertConfigVo vo = new FreightPriceAnalyseAlertConfigVo();
			if(StringUtils.isBlank(fromCity) && StringUtils.isBlank(toCity)){
				vo.setFromCity("");
				vo.setToCity("");
				vo.setFromCityName("");
				vo.setToCityName("");
			}//中文/拼音
			if(StringUtils.isNotBlank(fromCity) && StringUtils.isNotBlank(toCity) && 
					!StringUtils.equalsIgnoreCase(fromCity, "中文/拼音") && !StringUtils.equalsIgnoreCase(toCity, "中文/拼音")){
			//	PortEntityVO pevo = portApplication.findPortEntityByCityName(fromCity.trim());
			//	PortEntityVO pevo2 = portApplication.findPortEntityByCityName(toCity.trim());
				vo.setFromCity(fromCityCode);
				vo.setToCity(toCityCode);
				CityEntityVO cityVO=cityQueryApplication.findByCityCode(fromCityCode);
				if(null!=cityVO){
					vo.setFromCityName(cityVO.getName());
				}else{
					vo.setFromCityName(fromCity);
				}
				
				CityEntityVO toCityVO=cityQueryApplication.findByCityCode(toCityCode);
				if(null!=toCityVO){
					vo.setToCityName(toCityVO.getName());
				}else{
					vo.setToCityName(toCity);
				}
				
			}
			this.bookingType = this.getChooseUpOrdown();
			if(StringUtils.isNotBlank(this.bookingType)){
				vo.setBookingType(Integer.parseInt(this.bookingType.trim()));
			}
			vo.setId(id);
			vo.setAirCompany(airCompany);
			vo.setTriggerCondition(triggerCondition);
			vo.setFutureDays(futureDays);
			vo.setAlertWay(alertWay);
			vo.setCreateTime(new Date());
			vo.setCreator(user.getLocalUserId());
			vo.setCreatorName(user.getUserName());
			
			vo.setStartTime(sdf.parse(effectiveFrom));
			vo.setEndTime(sdf.parse(effectiveTo));
			vo.setAlertWay(1);
			vo.setAlertType("0");
			//xiangsf 2013-3-22 验证业务上记录不重复，不重复指：不同用户， 不同航空公司，不同航线 
		/*	FreightPriceAnalyseAlertConfigVo oldConfig = freightPriceAnalyseAlertConfigApplication
					.getConfigByLine(vo.getAirCompany(), vo.getFromCity(),  vo.getToCity(), user.getIds());*/
			
//			//按条件查询
//			List list = this.freightPriceAnalyseAlertConfigApplication.getAllAnalyseAlertByC(
//					vo.getFromCityName(), vo.getToCityName(), vo.getStartTime(), vo.getEndTime(), 
//					vo.getAirCompany(), vo.getAlertWay(), vo.getFutureDays(), vo.getTriggerCondition(),vo.getCreator());
			
			//根据list的size测试除了‘创建时间’之外的所有条件是否有对应的记录
			//如果size==0则证明之前没有相同数据除了“创建时间”不同的记录则可以添加
			//如果size>0 则无法添加
//			System.out.println("===============================================================");
//			System.out.println("按条件查询--list=="+list.size());
//			System.out.println("===============================================================");
			FreightPriceAnalyseAlertConfigVo oldConfig = null;
			
			//不是修改操作就查询是否已存在配置
			if(!StringAndDateAndFileUtil.checkString(isUpdate)&& !isUpdate.equals("yes")){
				SourceEntityVO source = sourceApplication.findSourceEntityByName(vo.getAirCompany()).get(0);
				 oldConfig = freightPriceAnalyseAlertConfigApplication
						.getConfigByLine(source.getHbaseKey(), vo.getFromCity(),  vo.getToCity(), user.getLocalUserId());
			}
			if(oldConfig != null){//查询出的配置项不为空
				//提示用户，已存在重复记录
				this.data = String.format("已存在航线[%s]至[%s],有效期为[%s]至[%s]的配置项!", oldConfig.getFromCityName(), oldConfig.getToCityName(),
						sdf.format(oldConfig.getStartTime()), sdf.format(oldConfig.getEndTime()));
			}else{
				//==========原来代码 start
				this.freightPriceAnalyseAlertConfigApplication.saveOrUpdate(vo);
				this.data= "succes";
				
				this.efd=effectiveFrom;
	            this.etd=effectiveTo;
			}
		} catch (Exception e) {
			this.data=e.getMessage();
			e.printStackTrace();
		} 
		ServletActionContext.getResponse().getWriter().print(this.data);
	}
	
	/**
	 * 保存价差预警
	 * @throws Exception 
	 */
   public void saveSpreadAlertConfig() throws Exception{
	   SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	   LogonUserCertificate user = com.fare.taluo.auth.security.ContextLoginUser.getLoginUser();
		FreightPriceAnalyseAlertConfigVo vo = new FreightPriceAnalyseAlertConfigVo();
		if(StringUtils.isNotBlank(fromCity) && StringUtils.isNotBlank(toCity) && 
				!StringUtils.equalsIgnoreCase(fromCity, "中文/拼音") && !StringUtils.equalsIgnoreCase(toCity, "中文/拼音")){
//			PortEntityVO pevo = portApplication.findPortEntityByCityName(fromCity.trim());
					//	PortEntityVO pevo2 = portApplication.findPortEntityByCityName(toCity.trim());
						vo.setFromCity(fromCityCode);
						vo.setToCity(toCityCode);
						CityEntityVO cityVO=cityQueryApplication.findByCityCode(fromCityCode);
						if(null!=cityVO){
							vo.setFromCityName(cityVO.getName());
						}else{
							vo.setFromCityName(fromCity);
						}
						
						CityEntityVO toCityVO=cityQueryApplication.findByCityCode(toCityCode);
						if(null!=cityVO){
							vo.setToCityName(toCityVO.getName());
						}else{
							vo.setToCityName(toCity);
						}
		}
		if(StringUtils.isBlank(this.bookingType)){
			vo.setBookingType(1);
		}
		if(StringUtils.isNotBlank(this.bookingType)){
			vo.setBookingType(Integer.parseInt(this.bookingType.trim()));
		}
		
		vo.setId(id);
		SourceEntityVO sourceVo = sourceApplication.findByHbaseKey(companyCode);
		vo.setAirCompany(sourceVo.getName());
		vo.setCarrierKey(companyCode);
		vo.setTriggerCondition(triggerCondition);
		vo.setFutureDays(futureDays);
		vo.setAlertWay(alertWay);
		vo.setFlgDeleted(0);
		vo.setCreateTime(new Date());
		vo.setCreator(user.getLocalUserId());
		vo.setCreatorName(user.getUserName());
		vo.setFlightNo(flightNo);
		vo.setCabinLevel(cabinLevel);
		vo.setFlightDate(sdf.parse(flightDate));
		vo.setTriggerConditionSpread(triggerConditionSpread);
		vo.setContrastSourceKey(contrastSourceKey);
		SourceEntityVO source = sourceApplication.findByHbaseKey(contrastSourceKey);
		vo.setContrastSource(source.getName());
		vo.setAlertType("2");
		vo.setAlertWay(1);
		FreightPriceAnalyseAlertConfigVo oldConfig = null;
		
		//不是修改操作就查询是否已存在配置
		if(!StringAndDateAndFileUtil.checkString(isUpdate)&& !isUpdate.equals("yes")){
			SourceEntityVO source1 = sourceApplication.findSourceEntityByName(vo.getAirCompany()).get(0);
			/* oldConfig = freightPriceAnalyseAlertConfigApplication
					.getConfigByLine(source1.getHbaseKey(), vo.getFromCity(),  vo.getToCity(), user.getIds());*/
			oldConfig = freightPriceAnalyseAlertConfigApplication.getSpreadConfig(flightNo,flightDate,contrastSourceKey,user.getLocalUserId());
		}
		
		if(oldConfig != null){//查询出的配置项不为空
			//提示用户，已存在重复记录
			this.data = String.format("已存在航线[%s]至[%s]!", oldConfig.getFromCityName(), oldConfig.getToCityName());
		}else{
			//==========原来代码 start
			this.freightPriceAnalyseAlertConfigApplication.saveOrUpdate(vo);
			this.data= "succes";
			
			this.efd=effectiveFrom;
            this.etd=effectiveTo;
		}
		ServletActionContext.getResponse().getWriter().print(this.data);
   }
   
   /**
    * 保存舱位变动预警
 * @throws Exception 
    */
   public void saveCabinChangeAlertConfig() throws Exception{
	   SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	   LogonUserCertificate user = com.fare.taluo.auth.security.ContextLoginUser.getLoginUser();
		FreightPriceAnalyseAlertConfigVo vo = new FreightPriceAnalyseAlertConfigVo();
		if(StringUtils.isNotBlank(fromCity) && StringUtils.isNotBlank(toCity) && 
				!StringUtils.equalsIgnoreCase(fromCity, "中文/拼音") && !StringUtils.equalsIgnoreCase(toCity, "中文/拼音")){
//			PortEntityVO pevo = portApplication.findPortEntityByCityName(fromCity.trim());
					//	PortEntityVO pevo2 = portApplication.findPortEntityByCityName(toCity.trim());
						vo.setFromCity(fromCityCode);
						vo.setToCity(toCityCode);
						CityEntityVO cityVO=cityQueryApplication.findByCityCode(fromCityCode);
						if(null!=cityVO){
							vo.setFromCityName(cityVO.getName());
						}else{
							vo.setFromCityName(fromCity);
						}
						
						CityEntityVO toCityVO=cityQueryApplication.findByCityCode(toCityCode);
						if(null!=cityVO){
							vo.setToCityName(toCityVO.getName());
						}else{
							vo.setToCityName(toCity);
						}
		}
		if(StringUtils.isBlank(this.bookingType)){
			vo.setBookingType(1);
		}
		if(StringUtils.isNotBlank(this.bookingType)){
			vo.setBookingType(Integer.parseInt(this.bookingType.trim()));
		}
		vo.setId(id);
		SourceEntityVO sourceVo = sourceApplication.findByHbaseKey(companyCode);
		vo.setAirCompany(sourceVo.getName());
		vo.setFlgDeleted(0);
		vo.setCarrierKey(companyCode);
		vo.setAlertWay(alertWay);
		vo.setCreateTime(new Date());
		vo.setCreator(user.getLocalUserId());
		vo.setCreatorName(user.getUserName());
		vo.setFlightNo(flightNo);
		vo.setFlightDate(sdf.parse(flightDate));
		vo.setAlertType("1");
		vo.setAlertWay(1);
		
		FreightPriceAnalyseAlertConfigVo oldConfig = null;
		
		//不是修改操作就查询是否已存在配置
		if(!StringAndDateAndFileUtil.checkString(isUpdate)&& !isUpdate.equals("yes")){
			SourceEntityVO source = sourceApplication.findSourceEntityByName(vo.getAirCompany()).get(0);
			 /*oldConfig = freightPriceAnalyseAlertConfigApplication
					.getConfigByLine(source.getHbaseKey(), vo.getFromCity(),  vo.getToCity(), user.getIds());*/
			 oldConfig = freightPriceAnalyseAlertConfigApplication.getCabinChangeConfig(flightNo, flightDate, user.getLocalUserId());
		}
		if(oldConfig != null){//查询出的配置项不为空
			//提示用户，已存在重复记录
			this.data = String.format("已存在航线[%s]至[%s]", oldConfig.getFromCityName(), oldConfig.getToCityName()	);
		}else{
			//==========原来代码 start
			this.freightPriceAnalyseAlertConfigApplication.saveOrUpdate(vo);
			this.data= "succes";
			
			this.efd=effectiveFrom;
           this.etd=effectiveTo;
		}
		ServletActionContext.getResponse().getWriter().print(this.data);
	  
	   
   }
	
   /**
    * 竞争对比预警
 * @throws Exception 
    */
   public void saveCompeteContrastAlertConfig() throws Exception{
	   SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	   LogonUserCertificate user = com.fare.taluo.auth.security.ContextLoginUser.getLoginUser();
		FreightPriceAnalyseAlertConfigVo vo = new FreightPriceAnalyseAlertConfigVo();
		if(StringUtils.isNotBlank(fromCity) && StringUtils.isNotBlank(toCity) && 
				!StringUtils.equalsIgnoreCase(fromCity, "中文/拼音") && !StringUtils.equalsIgnoreCase(toCity, "中文/拼音")){
//			PortEntityVO pevo = portApplication.findPortEntityByCityName(fromCity.trim());
					//	PortEntityVO pevo2 = portApplication.findPortEntityByCityName(toCity.trim());
						vo.setFromCity(fromCityCode);
						vo.setToCity(toCityCode);
						CityEntityVO cityVO=cityQueryApplication.findByCityCode(fromCityCode);
						if(null!=cityVO){
							vo.setFromCityName(cityVO.getName());
						}else{
							vo.setFromCityName(fromCity);
						}
						
						CityEntityVO toCityVO=cityQueryApplication.findByCityCode(toCityCode);
						if(null!=cityVO){
							vo.setToCityName(toCityVO.getName());
						}else{
							vo.setToCityName(toCity);
						}
		}
		if(StringUtils.isBlank(this.bookingType)){
			vo.setBookingType(1);
		}
		if(StringUtils.isNotBlank(this.bookingType)){
			vo.setBookingType(Integer.parseInt(this.bookingType.trim()));
		}
		vo.setId(id);
		SourceEntityVO sourceVo = sourceApplication.findByHbaseKey(companyCode);
		vo.setAirCompany(sourceVo.getName());
		vo.setCarrierKey(companyCode);
		vo.setTriggerCondition(triggerCondition);
		vo.setFutureDays(futureDays);
		vo.setAlertWay(alertWay);
		vo.setCreateTime(new Date());
		vo.setCreator(user.getLocalUserId());
		vo.setCreatorName(user.getUserName());
		vo.setFlightNo(flightNo);
		vo.setCabinLevel(cabinLevel);
		vo.setFlightDate(sdf.parse(flightDate));
		vo.setFlgDeleted(0);
		vo.setContrastSourceKey(compareCompanyCode);
		SourceEntityVO source = sourceApplication.findByHbaseKey(compareCompanyCode);
		vo.setContrastSource(source.getName());
		vo.setContrastFlightNo(contrastFlightNo);
		vo.setContrastCabinLevel(contrastCabinLevel);
		vo.setAlertType("3");
		vo.setAlertWay(1);
		FreightPriceAnalyseAlertConfigVo oldConfig = null;
		
		//不是修改操作就查询是否已存在配置
		if(!StringAndDateAndFileUtil.checkString(isUpdate)&& !isUpdate.equals("yes")){
			SourceEntityVO source1 = sourceApplication.findSourceEntityByName(vo.getAirCompany()).get(0);
			 oldConfig = freightPriceAnalyseAlertConfigApplication
					.getContrastConfig(flightNo,contrastFlightNo, flightDate,user.getLocalUserId());
/*			 oldConfig = freightPriceAnalyseAlertConfigApplication
					 .getConfigByLine(source1.getHbaseKey(), vo.getFromCity(),  vo.getToCity(), user.getIds());
*/		}
		if(oldConfig != null){//查询出的配置项不为空
			//提示用户，已存在重复记录
			this.data = String.format("已存在航线[%s]至[%s]", oldConfig.getFromCityName(), oldConfig.getToCityName());
		}else{
			//==========原来代码 start
			this.freightPriceAnalyseAlertConfigApplication.saveOrUpdate(vo);
			this.data= "succes";
			
			this.efd=effectiveFrom;
           this.etd=effectiveTo;
		}
		ServletActionContext.getResponse().getWriter().print(this.data);
	   
   }
	/**
	 * 修改预警配置（更新状态）
	 * <p>晕，新增修改不是一个事件
	 * @throws Exception
	 */
   public void updateFreightPriceAnalyseAlertConfigStats() throws Exception{
	   try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			LogonUserCertificate user = com.fare.taluo.auth.security.ContextLoginUser.getLoginUser();
			//User user = (User) getSession().get("currentUser");
			FreightPriceAnalyseAlertConfigVo vo = new FreightPriceAnalyseAlertConfigVo();
			
			if(StringUtils.isBlank(fromCity) && StringUtils.isBlank(toCity)){
				vo.setFromCity("");
				vo.setToCity("");
				vo.setFromCityName("");
				vo.setToCityName("");
			}//中文/拼音
			
			if(StringUtils.isNotBlank(fromCity) && StringUtils.isNotBlank(toCity) && 
					!StringUtils.equalsIgnoreCase(fromCity, "中文/拼音") && !StringUtils.equalsIgnoreCase(toCity, "中文/拼音")){
//				PortEntityVO pevo = portApplication.findPortEntityByCityName(fromCity.trim());
				//	PortEntityVO pevo2 = portApplication.findPortEntityByCityName(toCity.trim());
					vo.setFromCity(fromCityCode);
					vo.setToCity(toCityCode);
					CityEntityVO cityVO=cityQueryApplication.findByCityCode(fromCityCode);
					if(null!=cityVO){
						vo.setFromCityName(cityVO.getName());
					}else{
						vo.setFromCityName(fromCity);
					}
					
					CityEntityVO toCityVO=cityQueryApplication.findByCityCode(toCityCode);
					if(null!=cityVO){
						vo.setToCityName(toCityVO.getName());
					}else{
						vo.setToCityName(toCity);
					}
			}
			
			if(StringUtils.isBlank(this.bookingType)){
				vo.setBookingType(1);
			}
			if(StringUtils.isNotBlank(this.bookingType)){
				vo.setBookingType(Integer.parseInt(this.bookingType.trim()));
			}
			vo.setId(id);
			vo.setCreateTime(sdf.parse(oldcreateTime));
			vo.setCreator(creator);
			vo.setCreatorName(creatorName);
			
			vo.setStartTime(sdf.parse(effectiveFrom));
			vo.setEndTime(sdf.parse(effectiveTo));
			vo.setFlgDeleted(0);
			
			vo.setUpdateTime(new Date());
			vo.setUpdator(user.getLocalUserId());
			vo.setUpdatorName(user.getUserName());
			vo.setAirCompany(airCompany);
			vo.setAlertWay(alertWay);
			vo.setFutureDays(futureDays);
			vo.setTriggerCondition(triggerCondition);
			vo.setAlertWay(1);
			//xiangsf 2013-3-22 验证业务上记录不重复，不重复指：不同用户， 不同航空公司，不同航线 
			FreightPriceAnalyseAlertConfigVo oldConfig = null;
			
			//不是修改操作就查询是否已存在配置
			if(!StringAndDateAndFileUtil.checkString(isUpdate)&& !isUpdate.equals("yes")){
				 oldConfig = freightPriceAnalyseAlertConfigApplication
						.getConfigByLine(vo.getAirCompany(), vo.getFromCity(),  vo.getToCity(), user.getLocalUserId());
			}
			if(oldConfig != null && oldConfig.getId().longValue() != vo.getId().longValue()){//查询出的配置项不为空，同时又不是当前正在保存的配置对象
				//提示用户，已存在重复记录
				this.data = String.format("已存在航线[%s]至[%s],有效期为[%s]至[%s]的配置项!", oldConfig.getFromCityName(), oldConfig.getToCityName(),
						sdf.format(oldConfig.getStartTime()), sdf.format(oldConfig.getEndTime()));
			}else{
				//==========原来代码 start
				this.freightPriceAnalyseAlertConfigApplication.saveOrUpdate(vo);
				this.data= "succes";
				
				this.efd=effectiveFrom;
	            this.etd=effectiveTo;
			}
		} catch (Exception e) {
			this.data=e.getMessage();
			e.printStackTrace();
		} 
		ServletActionContext.getResponse().getWriter().print(this.data);
	   }
	   
	//删除预警配置（更新状态）
   public String deleteFreightPriceAnalyseAlertConfigStats() throws Exception{
	   this.freightPriceAnalyseAlertConfigApplication.delete(this.id);
	   this.returnPageURL = "freightPriceAnalyseAlertConfigAction!getAllFreightPriceAnalyseAlertConfig.action";
	   return "redirect";
   }
	
	public String getAllFreightPriceAnalyseAlertConfig(){
		init();
		LogonUserCertificate user = com.fare.taluo.auth.security.ContextLoginUser.getLoginUser();
		this.returnPageURL = "/fare/rateAnalysis/_yunjiayujing.jsp";
		Locale locale =super.getLocale();
		this.listvo = this.freightPriceAnalyseAlertConfigApplication.getAllAnalyseAlert(user.getLocalUserId(),locale);
		total = (listvo.size()%pageSize==0)?listvo.size()/pageSize:listvo.size()/pageSize+1;//总共分页数目
		//默认通过createTime排序
		Collections.sort(listvo, new Comparator<FreightPriceAnalyseAlertConfigVo>(){
			@Override
			public int compare(FreightPriceAnalyseAlertConfigVo o1,
					FreightPriceAnalyseAlertConfigVo o2) {
				return o2.getCreateTime().compareTo(o1.getCreateTime());
			}
		});
		count += flag; 
		if(count > 1){
			begin = (count-1) * pageSize;
			end = count * pageSize;  
		}else{
			count = 1;
			begin = 0;
			end = 14;
		}
		return "dispatcher";
	}

	
	
	
	//组合条件查询
	public String getAllFreightPriceAnalyseAlertConfigByC() throws ServletException, IOException{
		LogonUserCertificate user = com.fare.taluo.auth.security.ContextLoginUser.getLoginUser();
		
		//保存页面数据，以回显作用
		
		init();
		this.returnPageURL = "/fare/rateAnalysis/_yunjiayujing.jsp";
		
		try {
			DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			
			if(null!=this.effectiveFrom && null!=this.effectiveTo){
				this.efd = this.effectiveFrom;
				this.etd = this.effectiveTo;
			}
			
            if(StringUtils.equalsIgnoreCase(fromCity, "中文/拼音"))
            	fromCity="";
            
            if(StringUtils.equalsIgnoreCase(toCity,"中文/拼音"))
            	toCity="";
            
            //若effectiveFrom为null时，就不可以用sdf来parse，因此用null来代替
			/*this.listvo = this.freightPriceAnalyseAlertConfigApplication.getAllAnalyseAlertByC(
					fromCity, toCity, 
					("".equals(effectiveFrom)||effectiveFrom==null)?null:sdf.parse(effectiveFrom),
					("".equals(effectiveTo)||effectiveTo==null)?null:sdf.parse(effectiveTo), 
					airCompany,alertWay,futureDays,triggerCondition,user.getIds());*/
            this.listvo = this.freightPriceAnalyseAlertConfigApplication.getAllAnalyseAlertByC(
					 alertType , createTime,endCreateTime,user.getLocalUserId());
            if(createTime!=null){
            	  createTimeStr = DateUtils.dateToStr(createTime, "yyyy-MM-dd");
            }
            if(endCreateTime != null){
            	  endCreateTimeStr = DateUtils.dateToStr(endCreateTime, "yyyy-MM-dd");
            }
			count += flag; 
			if(count > 1){
				begin = (count-1) * pageSize;
				if(begin < listvo.size()){
					end = count * pageSize;
				}else{
					
				}
				  
			}else{
				count = 1;
				begin = 0;
				end = this.listvo.size();
			}
			
			//默认通过createTime排序
			Collections.sort(listvo, new Comparator<FreightPriceAnalyseAlertConfigVo>(){
				@Override
				public int compare(FreightPriceAnalyseAlertConfigVo o1,
						FreightPriceAnalyseAlertConfigVo o2) {
					return o2.getCreateTime().compareTo(o1.getCreateTime());
				}
			});
			
			JsonConfig jsc = new JsonConfig();
			jsc.registerJsonValueProcessor(Date.class,new DateJsonValueProcessor("yyyy-MM-dd HH:mm:ss"));
			this.data=net.sf.json.JSONArray.fromObject(this.listvo,jsc).toString();
//			if(listvo==null||listvo.size()==0)
//				message="fail";
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
//		ServletActionContext.getResponse().getWriter().print(this.message);
		return "dispatcher";
	} 
	
	public String turnPage(){
		LogonUserCertificate user = com.fare.taluo.auth.security.ContextLoginUser.getLoginUser();
		try {
			FreightPriceAnalyseAlertConfigVo vo = new FreightPriceAnalyseAlertConfigVo();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			if(StringUtils.isBlank(fromCity) && StringUtils.isBlank(toCity)){
				vo.setFromCity("");
				vo.setToCity("");
				vo.setFromCityName("");
				vo.setToCityName("");
			}//中文/拼音
			if(StringUtils.isNotBlank(fromCity) && StringUtils.isNotBlank(toCity) && 
					!StringUtils.equalsIgnoreCase(fromCity, "中文/拼音") && !StringUtils.equalsIgnoreCase(toCity, "中文/拼音")){
//				PortEntityVO pevo = portApplication.findPortEntityByCityName(fromCity.trim());
				//	PortEntityVO pevo2 = portApplication.findPortEntityByCityName(toCity.trim());
					vo.setFromCity(fromCityCode);
					vo.setToCity(toCityCode);
					CityEntityVO cityVO=cityQueryApplication.findByCityCode(fromCityCode);
					if(null!=cityVO){
						vo.setFromCityName(cityVO.getName());
					}else{
						vo.setFromCityName(fromCity);
					}
					
					CityEntityVO toCityVO=cityQueryApplication.findByCityCode(toCityCode);
					if(null!=cityVO){
						vo.setToCityName(toCityVO.getName());
					}else{
						vo.setToCityName(toCity);
					}
			}
			vo.setStartTime(sdf.parse(effectiveFrom));
			vo.setEndTime(sdf.parse(effectiveTo));
			vo.setAirCompany(airCompany);
			vo.setAlertWay(alertWay);
			vo.setFutureDays(futureDays);
			vo.setTriggerCondition(triggerCondition);
			this.listvo = this.freightPriceAnalyseAlertConfigApplication.getAllAnalyseAlertByC(
					 alertType , createTime,endCreateTime,user.getLocalUserId());
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return "";
	}
	
	//通过id取得修改的数据
	public String getFreightPriceAnalyseAlertConfigById(){
		init();
		Long id=Long.parseLong(request.getParameter("id"));
		vo=freightPriceAnalyseAlertConfigApplication.getFreightPriceAnalyseAlertConfig(id);
		String url = "";
		isUpdate = "yes";
		if(StringAndDateAndFileUtil.checkString(alertType)){
			if(alertType.equals("0")){//最低价预警
				url = "FreightPriceAlertAdd.jsp";
			}else if(alertType.equals("1")){//舱位变动预警
				url = "CabinChangeAlertAdd.jsp";
			}else if(alertType.equals("2")){//价差预警
				url = "SpreadAlertAdd.jsp";
			}else if(alertType.equals("3")){//竞争对比预警
				url = "CompeteContrastAlertAdd.jsp";
			}	
		}
		this.returnPageURL = "/fare/rateAnalysis/"+url;
		return "dispatcher";
		//this.returnPageURL = "/fare/rateAnalysis/update.jsp";
	}
	
	
	//点击新增时，再次初始化下拉列表 
	public String whenAddinitSelect(){
		init();
		this.returnPageURL="/fare/rateAnalysis/add.jsp";
		//this.returnPageURL="/fare/rateAnalysis/FreightPriceAlertAdd.jsp";
		return "dispatcher";
	}
	
	public String toJsp(){
		init();
		//this.returnPageURL="/fare/rateAnalysis/add.jsp";
		this.returnPageURL="/fare/rateAnalysis/FreightPriceAlertAdd.jsp";
		return "dispatcher";
	}
	
	public String toCabinChangeAddJsp(){
		Locale locale =super.getLocale();
		language=locale.toString();
		if(!language.equalsIgnoreCase("zh_CN") && !language.equalsIgnoreCase("en_US")){
			language="other";
		}
		this.returnPageURL="/fare/rateAnalysis/CabinChangeAlertAdd.jsp";
		return "dispatcher";
	}
	
	public String toSpreadAlertAddJsp(){
		Locale locale =super.getLocale();
		language=locale.toString();
		if(!language.equalsIgnoreCase("zh_CN") && !language.equalsIgnoreCase("en_US")){
			language="other";
		}
		this.returnPageURL="/fare/rateAnalysis/SpreadAlertAdd.jsp";
		return "dispatcher";
	}
	
	public String toCompeteContrastAlertAddJsp(){
		Locale locale =super.getLocale();
		language=locale.toString();
		if(!language.equalsIgnoreCase("zh_CN") && !language.equalsIgnoreCase("en_US")){
			language="other";
		}
		this.returnPageURL="/fare/rateAnalysis/CompeteContrastAlertAdd.jsp";
		return "dispatcher";
	}
	
	public String showSpread(){
		init();
		//this.returnPageURL="/fare/rateAnalysis/add.jsp";
		this.returnPageURL="/fare/rateAnalysis/SpreadAlertAdd.jsp";
		
		return "dispatcher";
	}
	
	public Long getId() {
		return id;
	}
	
	public void setId(Long id) {
		this.id = id;
	}

	public String getEfd() {
		return efd;
	}

	public void setEfd(String efd) {
		this.efd = efd;
	}

	public String getEtd() {
		return etd;
	}

	public void setEtd(String etd) {
		this.etd = etd;
	}

	public String getFlightFrom() {
		return flightFrom;
	}

	public void setFlightFrom(String flightFrom) {
		this.flightFrom = flightFrom;
	}

	public String getFlightTo() {
		return flightTo;
	}

	public void setFlightTo(String flightTo) {
		this.flightTo = flightTo;
	}

	public String getEffectiveFrom() {
		return effectiveFrom;
	}

	public void setEffectiveFrom(String effectiveFrom) {
		this.effectiveFrom = effectiveFrom;
	}

	public String getEffectiveTo() {
		return effectiveTo;
	}

	public void setEffectiveTo(String effectiveTo) {
		this.effectiveTo = effectiveTo;
	}

	public String getAirType() {
		return airType;
	}

	public void setAirType(String airType) {
		this.airType = airType;
	}

	public String getBookingType() {
		return bookingType;
	}

	public void setBookingType(String bookingType) {
		this.bookingType = bookingType;
	}

	public String getData() {
		return data;
	}

	public void setData(String data) {
		this.data = data;
	}
	public PortApplication getPortApplication() {
		return portApplication;
	}

	public void setPortApplication(PortApplication portApplication) {
		this.portApplication = portApplication;
	}
	
	public List<FreightPriceAnalyseAlertConfigVo> getListvo() {
		return listvo;
	}

	public void setListvo(List<FreightPriceAnalyseAlertConfigVo> listvo) {
		this.listvo = listvo;
	}

	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}

	public int getBegin() {
		return begin;
	}

	public void setBegin(int begin) {
		this.begin = begin;
	}

	public int getEnd() {
		return end;
	}

	public void setEnd(int end) {
		this.end = end;
	}

	public int getFlag() {
		return flag;
	}

	public void setFlag(int flag) {
		this.flag = flag;
	}

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}

	public FreightPriceAnalyseAlertConfigVo getVo() {
		return vo;
	}

	public void setVo(FreightPriceAnalyseAlertConfigVo vo) {
		this.vo = vo;
	}

	public Long getCreator() {
		return creator;
	}

	public void setCreator(Long creator) {
		this.creator = creator;
	}

	public String getCreatorName() {
		return creatorName;
	}

	public void setCreatorName(String creatorName) {
		this.creatorName = creatorName;
	}

	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public FreightPriceAnalyseAlertConfigVo getFvo() {
		return fvo;
	}

	public void setFvo(FreightPriceAnalyseAlertConfigVo fvo) {
		this.fvo = fvo;
	}

	public int getFutureDays() {
		return futureDays;
	}

	public void setFutureDays(int futureDays) {
		this.futureDays = futureDays;
	}

	public int getTriggerCondition() {
		return triggerCondition;
	}

	public void setTriggerCondition(int triggerCondition) {
		this.triggerCondition = triggerCondition;
	}

	public String getAirCompany() {
		return airCompany;
	}

	public void setAirCompany(String airCompany) {
		this.airCompany = airCompany;
	}

	public List<AirCompanyEntityVO> getSourceList() {
		return sourceList;
	}

	public void setSourceList(List<AirCompanyEntityVO> sourceList) {
		this.sourceList = sourceList;
	}

	public int getAlertWay() {
		return alertWay;
	}

	public void setAlertWay(int alertWay) {
		this.alertWay = alertWay;
	}

	public int getPageSize() {
		return pageSize;
	}

	public List<SourceEntityVO> getTempList() {
		return tempList;
	}

	public void setTempList(List<SourceEntityVO> tempList) {
		this.tempList = tempList;
	}

	public String getOldcreateTime() {
		return oldcreateTime;
	}

	public void setOldcreateTime(String oldcreateTime) {
		this.oldcreateTime = oldcreateTime;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public int getQueryJump() {
		return queryJump;
	}

	public void setQueryJump(int queryJump) {
		this.queryJump = queryJump;
	}

	public List<SourceEntityVO> getSourceVOList() {
		return sourceVOList;
	}

	public void setSourceVOList(List<SourceEntityVO> sourceVOList) {
		this.sourceVOList = sourceVOList;
	}

	public String getFlightNo() {
		return flightNo;
	}

	public void setFlightNo(String flightNo) {
		this.flightNo = flightNo;
	}

	public String getCarrierKey() {
		return carrierKey;
	}

	public void setCarrierKey(String carrierKey) {
		this.carrierKey = carrierKey;
	}
	
	

	public String getCreateTimeStr() {
		return createTimeStr;
	}

	public void setCreateTimeStr(String createTimeStr) {
		this.createTimeStr = createTimeStr;
	}

	public String getCabinLevel() {
		return cabinLevel;
	}

	public void setCabinLevel(String cabinLevel) {
		this.cabinLevel = cabinLevel;
	}

	public String getFlightDate() {
		return flightDate;
	}

	public void setFlightDate(String flightDate) {
		this.flightDate = flightDate;
	}

	public String getCompanyCode() {
		return companyCode;
	}

	public void setCompanyCode(String companyCode) {
		this.companyCode = companyCode;
	}

	public int getTriggerConditionSpread() {
		return triggerConditionSpread;
	}

	public void setTriggerConditionSpread(int triggerConditionSpread) {
		this.triggerConditionSpread = triggerConditionSpread;
	}

	public String getContrastSourceKey() {
		return contrastSourceKey;
	}

	public void setContrastSourceKey(String contrastSourceKey) {
		this.contrastSourceKey = contrastSourceKey;
	}

	public String getCompareCompanyCode() {
		return compareCompanyCode;
	}

	public void setCompareCompanyCode(String compareCompanyCode) {
		this.compareCompanyCode = compareCompanyCode;
	}

	public String getContrastFlightNo() {
		return contrastFlightNo;
	}

	public void setContrastFlightNo(String contrastFlightNo) {
		this.contrastFlightNo = contrastFlightNo;
	}

	public String getContrastCabinLevel() {
		return contrastCabinLevel;
	}

	public void setContrastCabinLevel(String contrastCabinLevel) {
		this.contrastCabinLevel = contrastCabinLevel;
	}

	public String getAlertType() {
		return alertType;
	}

	public void setAlertType(String alertType) {
		this.alertType = alertType;
	}

	public String getIsUpdate() {
		return isUpdate;
	}

	public void setIsUpdate(String isUpdate) {
		this.isUpdate = isUpdate;
	}

	public Date getEndCreateTime() {
		return endCreateTime;
	}

	public void setEndCreateTime(Date endCreateTime) {
		this.endCreateTime = endCreateTime;
	}

	public String getEndCreateTimeStr() {
		return endCreateTimeStr;
	}

	public void setEndCreateTimeStr(String endCreateTimeStr) {
		this.endCreateTimeStr = endCreateTimeStr;
	}

	public String getChooseUpOrdown() {
		return chooseUpOrdown;
	}

	public void setChooseUpOrdown(String chooseUpOrdown) {
		this.chooseUpOrdown = chooseUpOrdown;
	}

	public String getFromCity() {
		return fromCity;
	}

	public void setFromCity(String fromCity) {
		this.fromCity = fromCity;
	}

	public String getToCity() {
		return toCity;
	}

	public void setToCity(String toCity) {
		this.toCity = toCity;
	}

	public String getFromCityCode() {
		return fromCityCode;
	}

	public void setFromCityCode(String fromCityCode) {
		this.fromCityCode = fromCityCode;
	}

	public String getToCityCode() {
		return toCityCode;
	}

	public void setToCityCode(String toCityCode) {
		this.toCityCode = toCityCode;
	}

	public String getLanguage() {
		return language;
	}

	public void setLanguage(String language) {
		this.language = language;
	}
	
}
